﻿using System;
using System.Collections.Specialized;

namespace _5E.Infrastructure.Collections
{
    public static class NameValueCollectionExtension
    {
        public static T ExtractFor<T>(this NameValueCollection queryArguments, string key)
        {
            try
            {
                if (typeof(T) == typeof(Guid))
                    return (T)(new Guid(queryArguments[key]) as object);
                else if (typeof(T).IsEnum)
                    return (T)System.Enum.Parse(typeof(T), queryArguments[key]);
                return (T)Convert.ChangeType(queryArguments[key], typeof(T));
            }
            catch
            {
                return default(T);
            }
        }
    }
}
